package org.miyuki4j.cfgtool;

import org.miyuki4j.cfgtool.exchanger.JsonTableDataExchanger;
import org.miyuki4j.cfgtool.exchanger.XmlTableDataExchanger;

import java.util.Arrays;

/**
 * @Desc
 * @Author miyuki4j
 * @Date 2025/7/24/星期四 17:11
 * @Version 1.0
 */
public enum TableDataExchangerEnum {

    XML("xml", new XmlTableDataExchanger()),

    JSON("json", new JsonTableDataExchanger()),

    ALL("all", TableDataExchanger.NONE),
    ;
    private final String type;

    private final TableDataExchanger exchanger;

    TableDataExchangerEnum(String type, TableDataExchanger exchanger) {
        this.type = type;
        this.exchanger = exchanger;
    }

    public static TableDataExchangerEnum of(String value) {
        return Arrays.stream(values())
                .filter(exchangerEnum -> exchangerEnum.type.equals(value))
                .findFirst()
                .orElse(null);
    }

    public String getType() {
        return type;
    }

    public TableDataExchanger getExchanger() {
        return exchanger;
    }

    public TableDataExporter getExporter() {
        return exchanger;
    }

    public TableDataImporter getImporter() {
        return exchanger;
    }

    public String concatFileName(String className) {
        return className + "." + type;
    }

    public boolean isFileType(String fileName) {
        return fileName.endsWith("." + type);
    }

    public String cutClassName(String fileName) {
        return fileName.substring(0, fileName.lastIndexOf("."));
    }
}
